/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __was_resp_object.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/11 by jikw                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include <corba/corba_comm.h>
#include <corba/corba_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "frlist.h"
#include "frreport_comm.h"
#include "apmtest_cunit.h"
#include "apmtest_comm.h"

#include "../../../src/module/corba/config.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __was_resp_object(Frbyte_Packet *p_st_data
                              , Frreport_body_data *p_st_body_data
                              , char *p_key
                              , int i_pos);

static void case1(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = NULL;
    int i_pos = 0;
    int i_ret = 0;

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case2(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = (char*)"response.ejb.function.1.1";
    int i_pos = 0;
    int i_ret = 0;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x01", 1);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

static void case3(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    char *p_key = NULL;
    int i_pos = 0;
    int i_ret = 0;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x01", 1);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case4(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = (char*)"response.ejb.function.1.1";
    int i_pos = 0;
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x00", 1);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case5(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = (char*)"response.ejb.function.1.1";
    int i_pos = 0;
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x01\x02\x03\x04", 4);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case6(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = (char*)"response.ejb.function.1.1";
    int i_pos = 0;
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x7f\xff\xff\x02\x00\x00\x00\x23\x49\x44\x4c\x3a\x6f\x6d\x67\x2e\x6f\x72\x67\x2f\x43\x4f\x52\x42\x41\x2f\x57\x53\x74\x72\x69\x6e\x67\x56\x61\x6c\x75\x65\x3a\x31\x2e\x30\x00\xbd\x00\x00\x00\x16\x00\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x2c\x00\x20\x00\x65\x00\x6a\x00\x62\x00\x21", 70);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case7(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = (char*)"response.ejb.function.1.1";
    int i_pos = 0;
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x7f\xff\xff\x02\x00\x00\x00\x3a\x52\x4d\x49\x3a\x5b\x4c\x63\x6f\x6d\x2e\x65\x6a\x62\x2e\x45\x6d\x70\x6c\x6f\x79\x65\x65\x3b\x3a\x30\x31\x32\x43\x43\x35\x30\x35\x42\x30\x35\x39\x44\x46\x42\x41\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x37\x35\x42\x43\x44\x31\x35\x00\xbd\xbd\x00\x00\x00\x03\x7f\xff\xff\x02\x00\x00\x00\x37\x52\x4d\x49\x3a\x63\x6f\x6d\x2e\x65\x6a\x62\x2e\x45\x6d\x70\x6c\x6f\x79\x65\x65\x3a\x30\x31\x32\x43\x43\x35\x30\x35\x42\x30\x35\x39\x44\x46\x42\x41\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x37\x35\x42\x43\x44\x31\x35\x00\xbd\x00\x00\x04\xd2\x00\x00\x02\x00\x00\xbd\xbd\xbd\xbd\xbd\xbd\xbd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbd\xbd\xbd\xbd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbd\xbd\x00\x00\x00\x00\x7f\xff\xff\x02\x00\x00\x00\x23\x49\x44\x4c\x3a\x6f\x6d\x67\x2e\x6f\x72\x67\x2f\x43\x4f\x52\x42\x41\x2f\x57\x53\x74\x72\x69\x6e\x67\x56\x61\x6c\x75\x65\x3a\x31\x2e\x30\x00\xbd\x00\x00\x00\x06\x00\x43\x00\x45\x00\x4f\xbd\xbd\x00\x00\x00\x00\x7f\xff\xff\x02\xff\xff\xff\xff\xff\xff\xff\xc0\x00\x00\x00\x10\x00\x73\x00\x75\x00\x70\x00\x65\x00\x72\x00\x6d\x00\x61\x00\x6e\x7f\xff\xff\x02\xff\xff\xff\xff\xff\xff\xff\x30\x00\x00\x10\xe1\x00\x00\x02\x00\x69\xbd\xbd\xbd\xbd\xbd\xbd\xbd\x40\xb0\xe1\x22\xad\x81\xad\xeb\x43\xd8\x0e\x8a\xbd\xbd\xbd\xbd\x00\x00\x00\x00\x19\xc1\x87\x8a\x00\x2b\xbd\xbd\x00\x00\x00\x00\x7f\xff\xff\x02\xff\xff\xff\xff\xff\xff\xff\x64\x00\x00\x00\x06\x00\x43\x00\x54\x00\x4f\xbd\xbd\x00\x00\x00\x00\x7f\xff\xff\x02\xff\xff\xff\xff\xff\xff\xff\x48\x00\x00\x00\x12\x96\x33\x51\x49\x76\x84\x54\x73\x90\x53\x00\x61\x00\x61\x00\x62\x00\x62\xbd\xbd\x7f\xff\xff\x02\xff\xff\xff\xff\xff\xff\xfe\xb4\x00\x00\x10\xe1\x01\x6b\x02\x00\x69\xbd\xbd\xbd\x40\xb0\xe1\x22\xad\x81\xad\xeb\x43\xd8\x0e\x8a\xbd\xbd\xbd\xbd\x00\x00\x00\x00\x19\xc1\x87\x8a\x00\x2b\xbd\xbd\x7f\xff\xff\x02\x00\x00\x00\x18\x52\x4d\x49\x3a\x5b\x42\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x00\x00\x00\x00\x04\x74\x65\x73\x74\xff\xff\xff\xff\xff\xff\xff\x60\x00\x00\x00\x00\x7f\xff\xff\x02\xff\xff\xff\xff\xff\xff\xfe\xbc\x00\x00\x00\x08\x9e\xc4\x91\xd1\x75\x30\x56\xed", 528);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case8(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_key = (char*)"response.ejb.function.1.1";
    int i_pos = 0;
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    //Frreport_data *p_st_report_data = NULL;
    //char *p_show_data = NULL;
    //unsigned int ui_show_data_len = 0;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x7f\xff\xff\x02\x00\x00\x00\x18\x52\x4d\x49\x3a\x5b\x53\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x00\x00\x00\x00\x03\x00\x01\x00\x02\x00\x37", 42);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __was_resp_object(p_st_data, p_st_body_data, p_key, i_pos);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    //p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    //frreport_data_add_body(p_st_report_data, p_st_body_data);
    //frdbg_report_data_format(p_st_report_data, FR_YES, &p_show_data, &ui_show_data_len);
    //printf("p_show_data:[%s]\n", p_show_data);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    //frreport_data_free(&p_st_report_data);
    //frfree(p_show_data);

    return;
}

int add_suite__was_resp_object(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __was_resp_object()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);
    CU_add_test(pSuite, "case7", case7);
    CU_add_test(pSuite, "case8", case8);

    return FR_SUCC;

}


